
jsx3.Class.defineClass("ux.cdb.admin.AdminDispatcher", null, null, function(dispatcher) {
    //PubSub Events of load some stage
    dispatcher.EVENT_LOADSTAGE = "ux.cdb.admin.AdminDispatcher.EVENT_LOADSTAGE";

    dispatcher._container = null;

    dispatcher._currentStage = null;

    dispatcher.setMainContainer = function(c) {
        dispatcher._container = c;
    };

    dispatcher.getMainContainer = function() {
        return dispatcher._container;
    };

    dispatcher.loadStageByMessage = function(msg) {
		//do some modification for flexible params passing...
		//because we need params for different module
		/* Eric old code
        if(!dispatcher._container) { return false; }
        var stageModule = msg.module;
        var stageEntry = msg.entry;
		console.log(msg);
        dispatcher.loadStage(stageModule, stageEntry);*/
		//begin alan's wonderful future..
		
		var ignoreList = ['pubSubTimestamp','subject','target','module','entry','jsxid','jsxtext'];
		var isInList = function(str){
			for(var i=0;i<ignoreList.length;i++){
				if(str == ignoreList[i])	return true;
			}
			return false;
		}
		if(!dispatcher._container) { return false; }
		var params = [msg.module,msg.entry];
		for(var prop in msg){
			if(!isInList(prop))
				params.push(msg[prop]);
			
		}
		dispatcher.loadStage.apply(dispatcher,params);
		
		//dispatcher.loadStage(params);
    };

    dispatcher.loadStage = function(module, entry) {
		//rearrange params for pass into IStage.getInstance method
        //ignore the 'module' and 'entry' argument,because is useless for getInstance method.
		var params = Array.prototype.slice.call(arguments, 2);
		
        //Load stage entry UI into main container of application
        //ux.megatron.Main.load
        var error = false, i;
        if(dispatcher._currentStage != (Array.prototype.join.call(arguments, "::"))) {
            var modules = module.split("/");
            var realModule = ux.cdb.admin;
            for(i = 0; (i < modules.length) && realModule; i ++){
                realModule = realModule[modules[i]];
            }
            if(!realModule || !realModule[entry]) {
                var srcPath = "ux/cdb/admin/"+ module + "/" + entry + ".js";
                try{
                    ux.megatron.Main.loadClass(srcPath, "ux.cdb.admin");
                } catch(ex){
                    alert("ERROR: Can not find file at path " + srcPath);
                    error = true;
                }
            }
            if(!error){
                realModule = ux.cdb.admin;
                for(i = 0; i < modules.length; i ++){
                    realModule = realModule[modules[i]];
                }
                if(realModule){
                    //var stageInstance = realModule[entry].getInstance();
					//modify by alan.for pass params purpose.
					var stageInstance = realModule[entry].getInstance.apply(realModule[entry],params);
                    this._container.removeChildren();
                    stageInstance.loadComponent(this._container);
                    dispatcher._currentStage = Array.prototype.join.call(arguments, "::");
                } else {
                    alert("ERROR: Can not find class.");
                }
            }
        }
    };

    ux.megatron.PubSub.subscribe(dispatcher.EVENT_LOADSTAGE, dispatcher.loadStageByMessage);

});